package cc.git.liuyan.test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;

//足彩测试
public class Yb_Test_1 {
    public static WebElement getWebElement(WebDriverWait wait, String xpath) {
        return wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));
    }

    public static WebElement getWebElement(WebDriverWait wait, WebElement parent, String xpath) {
        return wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(parent, By.xpath(xpath)));
    }

    public static List<WebElement> getWebElementList(WebDriverWait wait, WebElement parent, String xpath) {
        return wait.until(new ExpectedCondition<List<WebElement>>() {
            public List<WebElement> apply(WebDriver webDriver) {
                return parent.findElements(By.xpath(xpath));
            }

            public String toString() {
                return String.format("visibility of element located by %s", xpath);
            }
        });
    }

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.edge.driver", "E:\\edgeDriver\\msedgedriver.exe");
        EdgeOptions edgeOptions = new EdgeOptions();
        //edgeOptions.addArguments("--headless"); //无头模式
        edgeOptions.addArguments("--remote-allow-origins=*");
        WebDriver driver = new EdgeDriver(edgeOptions);
        WebDriverWait defaultWait = new WebDriverWait(driver, Duration.ofSeconds(30));
        //
        driver.get("https://www.iwon42962.vip/games/PSport.aspx");
        WebElement f = getWebElement(defaultWait, "//*[@id=\"PSport\"]");
        driver.switchTo().frame(f);

        System.out.println("查数据......");
        WebElement webElement = getWebElement(defaultWait, "/html/body/div[3]/div[3]/div[5]/div/div[1]/div[3]/div/div/div[1]/div/h3");
        System.out.println(webElement.getText());

        WebElement webElement2 = getWebElement(defaultWait, "/html/body/div[3]/div[3]/div[4]/div[1]/div[2]/div[3]/div[2]/div[3]/table[1]/tbody/tr[1]/td[6]/div[2]/a[1]/span");
        System.out.println(webElement2.getText());

        System.out.println("来点稍微正式的......");
        WebElement typeTab = getWebElement(defaultWait, "/html/body/div[3]/div[3]/div[4]/div[1]/div[2]/div[3]/div[1]/div[2]/div[1]/div/h3");
        System.out.println(typeTab.getText());

        WebElement typeGroup = getWebElement(defaultWait, "/html/body/div[3]/div[3]/div[4]/div[1]/div[2]/div[3]/div[2]");
        List<WebElement> smList = getWebElementList(defaultWait, typeGroup, "./div | ./table");
        System.out.println(smList.size());
        if (smList.size() > 2) {
            for (int i = 0; i < smList.size(); i++) {
                if (i >= 2) {
                    if (i % 2 == 0) {
                        System.out.println(smList.get(i).getText() + "------------------------------------------------------------------------------------------------");
                    } else {
                        List<WebElement> scList = getWebElementList(defaultWait, smList.get(i), "./table");
//                        System.out.println(scList.size());
                        if (scList != null && scList.size() > 0) {
                            for (WebElement sc : scList) {
                                List<WebElement> xcList = getWebElementList(defaultWait, sc, "./tbody/tr[contains(@class, 'mkline')]");
                                String scSj = "";
                                if (xcList != null && xcList.size() > 0) {
                                    for (WebElement xc : xcList) {
                                        List<WebElement> scSjList = getWebElementList(defaultWait, xc, "./td[contains(@class, 'col-time ')]/span");
                                        if (scSj.isEmpty() && scSjList != null && scSjList.size() > 0) {
                                            for (WebElement sj : scSjList) {
                                                scSj = scSj + " / " + sj.getText();
                                            }
                                            scSj = scSj.substring(1);
                                            System.out.println("时间：" + scSj + "  内共有 " + xcList.size() + " 场");
                                        }
                                        List<WebElement> dmList = getWebElementList(defaultWait, xc, "./td[contains(@class, 'col-name')]/div/span");
                                        if (dmList == null || dmList.size() == 0) {
                                            continue;
                                        }
                                        List<WebElement> dmpxList = getWebElementList(defaultWait, xc, "./td[contains(@class, 'col-hdp hdp-0 status_1')]/div[contains(@class, 'odds')]/a/span");
                                        WebElement dmTag = getWebElement(defaultWait, xc, "./td[contains(@class, 'col-hdp hdp-0 status_1')]/div[contains(@class, 'hdp show-lock-hdp')]/span[normalize-space() != '']");

                                        if (dmList != null && dmList.size() > 0) {
                                            for (int i1 = 0; i1 < dmList.size(); i1++) {
                                                try {
                                                    System.out.printf("时间：%-15s  队伍：%-20s  让分盘Tag：%-8s  让分盘：%-6s %n", scSj, dmList.get(i1).getText(), dmTag == null ? "" : dmTag.getText(), dmpxList.get(i1).getText());
                                                    //System.out.println(dmList.get(i1).getText() + "=========" + dmpxList.get(i1).getText());
                                                } catch (Exception e) {
                                                    continue;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        driver.switchTo().defaultContent();
        System.out.println("111111111111111");
    }
}

